package net.web.controller;

import net.web.common.framework.simplemvc.RequestMapping;
import net.web.common.utils.DateUtils;
import net.web.common.utils.JSONUtil;
import net.web.common.utils.PageInfo;
import net.web.common.utils.Result;
import net.web.entity.Blog;
import net.web.entity.Vo.BlogVo;
import net.web.service.BlogService;
import net.web.service.impl.BlogServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RequestMapping("/api/blog")
public class BlogController {

    private BlogService blogService = new BlogServiceImpl();


    @RequestMapping("/pageList.do")
    public void pageList(HttpServletRequest req, HttpServletResponse resp) {
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        int pageNo = pageNoStr == null || pageNoStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        int pageSize = pageSizeStr == null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);

        Blog blog = new Blog();
        blog.setTitle(req.getParameter("title"));
        blog.setClassid(parseInt(req.getParameter("classid"), 0));
        blog.setAuthorid(parseInt(req.getParameter("authorid"), 0));
        blog.setIsfeatured(parseInt(req.getParameter("isfeatured"), -1));

        PageInfo<BlogVo> pageInfo = blogService.pageList(pageNo, pageSize, blog);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }


    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");

        Blog blog = new Blog();
        blog.setTitle(req.getParameter("title"));
        blog.setContent(req.getParameter("content"));
        blog.setExcerpt(req.getParameter("excerpt"));
        blog.setCoverimage(req.getParameter("coverimage"));

        blog.setClassid(parseInt(req.getParameter("classid"), 0));
        blog.setAuthorid(parseInt(req.getParameter("authorid"), 0));
        blog.setIsfeatured(parseInt(req.getParameter("isfeatured"), 0));
        blog.setIscommentable(parseInt(req.getParameter("iscommentable"), 1));

        blog.setMetatitle(req.getParameter("metatitle"));
        blog.setMetadescription(req.getParameter("metadescription"));
        blog.setMetakeywords(req.getParameter("metakeywords"));
        blog.setPublishedat(req.getParameter("publishedat"));
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        blog.setCreatedat(now);
        blog.setUpdatedat(now);

        if (blogService.insert(blog)) {
            JSONUtil.printByJSON(resp, Result.success("添加成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("添加失败"));
        }
    }

    private int parseInt(String val, int defaultVal) {
        if (val == null || val.trim().isEmpty()) return defaultVal;
        try {
            return Integer.parseInt(val.trim());
        } catch (NumberFormatException e) {
            return defaultVal;
        }
    }


    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        if (blogService.delete(id)) {
            JSONUtil.printByJSON(resp, Result.success("删除成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("删除失败"));
        }
    }


    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Blog blog = new Blog();
        blog.setId(Integer.parseInt(req.getParameter("id")));

        // 以下字段允许为空，为空则不更新
        String title = req.getParameter("title");
        if (title != null && !title.trim().isEmpty()) blog.setTitle(title);

        String content = req.getParameter("content");
        if (content != null && !content.trim().isEmpty()) blog.setContent(content);

        String excerpt = req.getParameter("excerpt");
        if (excerpt != null && !excerpt.trim().isEmpty()) blog.setExcerpt(excerpt);

        String coverimage = req.getParameter("coverimage");
        if (coverimage != null && !coverimage.trim().isEmpty()) blog.setCoverimage(coverimage);

        String classid = req.getParameter("classid");
        if (classid != null && !classid.trim().isEmpty()) blog.setClassid(Integer.parseInt(classid));

        String authorid = req.getParameter("authorid");
        if (authorid != null && !authorid.trim().isEmpty()) blog.setAuthorid(Integer.parseInt(authorid));

        String isfeatured = req.getParameter("isfeatured");
        if (isfeatured != null && !isfeatured.trim().isEmpty()) blog.setIsfeatured(Integer.parseInt(isfeatured));

        String iscommentable = req.getParameter("iscommentable");
        if (iscommentable != null && !iscommentable.trim().isEmpty()) blog.setIscommentable(Integer.parseInt(iscommentable));

        String metatitle = req.getParameter("metatitle");
        if (metatitle != null && !metatitle.trim().isEmpty()) blog.setMetatitle(metatitle);

        String metadescription = req.getParameter("metadescription");
        if (metadescription != null && !metadescription.trim().isEmpty()) blog.setMetadescription(metadescription);

        String metakeywords = req.getParameter("metakeywords");
        if (metakeywords != null && !metakeywords.trim().isEmpty()) blog.setMetakeywords(metakeywords);

        String publishedat = req.getParameter("publishedat");
        if (publishedat != null && !publishedat.trim().isEmpty()) blog.setPublishedat(publishedat);

        blog.setUpdatedat(String.valueOf(LocalDateTime.now()));

        if (blogService.update(blog)) {
            JSONUtil.printByJSON(resp, Result.success("更新成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("更新失败"));
        }
    }
}